package cn.sl.vlc;

import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/9/26.
 */
public class HandleUtil {
    private static Handler mHandler = new Handler(Looper.getMainLooper());
    private static HashMap<Byte, EmergentWordModel> emergentWordMap = new HashMap<>();
    private static int MAX_VOLUM = 255;
    private static String TEXT = "滚动字幕";

    public static void handleMsg(byte[] msg, final VlcVideoActivity mVlcVideoActivity) {
        try {
            byte[] buffer;
            switch (getType(msg)) {
                case 1:
                    handleEmergentWord(msg, mVlcVideoActivity);
                    break;
                case 2:
                    int k = msg[6];
                    if (k > 8) {
                        k = 8;
                    }
                    final int volum = k * MAX_VOLUM / 8;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mVlcVideoActivity.setVolum(volum);
                        }
                    });
                    break;
                case 4:
                    buffer = new byte[msg.length - 6];
                    System.arraycopy(msg, 6, buffer, 0, buffer.length);
                    final String s4 = new String(buffer, "utf-8");
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mVlcVideoActivity.setCurrentStation(s4);
                        }
                    });
                    break;
                case 5:
                    buffer = new byte[msg.length - 6];
                    System.arraycopy(msg, 6, buffer, 0, buffer.length);
                    final String s5 = new String(buffer, "utf-8");
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mVlcVideoActivity.setNextStation(s5);
                        }
                    });
                    break;
                case 7:
                    buffer = new byte[msg.length - 6];
                    System.arraycopy(msg, 6, buffer, 0, buffer.length);
                    final String s7 = new String(buffer, "utf-8");
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mVlcVideoActivity.setFinnalStation(s7);
                        }
                    });
                    break;
                case 6:
                    switch (msg[6]) {
                        case 0:
                            buffer = new byte[msg.length - 7];
                            System.arraycopy(msg, 7, buffer, 0, buffer.length);
                            TEXT = new String(buffer, "utf-8");
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    mVlcVideoActivity.setScrollText(TEXT);
                                }
                            });
                            break;
                        case 1:
                            buffer = new byte[msg.length - 7];
                            System.arraycopy(msg, 7, buffer, 0, buffer.length);
                            final int size = Integer.parseInt(new String(buffer, "utf-8"));
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    mVlcVideoActivity.setScrollTextSize(size);
                                }
                            });
                            break;
                        case 2:
                            break;
                        case 3:
                            buffer = new byte[msg.length - 7];
                            System.arraycopy(msg, 7, buffer, 0, buffer.length);
                            final int color = Color.parseColor(new String(buffer, "utf-8"));
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    mVlcVideoActivity.setScrollTextColor(color);
                                }
                            });
                            break;
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void handleEmergentWord(byte[] msg, final VlcVideoActivity mVlcVideoActivity) throws UnsupportedEncodingException {
        byte[] buffer;
        EmergentWordModel emergentWordModel = emergentWordMap.get(msg[6]);
        if (emergentWordModel == null) {
            emergentWordModel = new EmergentWordModel();
            emergentWordMap.put(msg[6], emergentWordModel);
        }
        switch (msg[7]) {
            case 1:
                buffer = new byte[msg.length - 8];
                System.arraycopy(msg, 8, buffer, 0, buffer.length);
                emergentWordModel.setText(new String(buffer, "utf-8"));
                break;
            case 2:
                final EmergentWordModel finalEmergentWordModel = emergentWordModel;
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mVlcVideoActivity.setEmergent(finalEmergentWordModel);
                    }
                });
                break;
            case 3:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mVlcVideoActivity.clearEmergent();
                    }
                });
                break;
            case 4:
                buffer = new byte[msg.length - 8];
                System.arraycopy(msg, 8, buffer, 0, buffer.length);
                emergentWordModel.setTextSize(Integer.parseInt(new String(buffer, "utf-8")));
                break;
            case 5:
                break;
            case 6:
                buffer = new byte[msg.length - 8];
                System.arraycopy(msg, 8, buffer, 0, buffer.length);
                emergentWordModel.setTextColor(Color.parseColor(new String(buffer, "utf-8")));
                break;
            case 7:
                buffer = new byte[msg.length - 8];
                System.arraycopy(msg, 8, buffer, 0, buffer.length);
                emergentWordModel.setBackColor(Color.parseColor(new String(buffer, "utf-8")));
                break;
        }
    }

    private static int getType(byte[] msg) {
        if (msg[0] == 102 && msg[1] == -86 && msg[2] == -86 && msg[3] == 102) {
            return msg[4];
        }
        return 0;
    }
}
